﻿using System.Linq;
using UnityEngine;


namespace BackpackSystem
{
    /// <summary>
    /// 
    /// </summary>
    public static class InventoryExtension
    {

        /// <summary>
        /// 
        /// </summary>
        public static bool TryGetEmptyItemSlot(this InventoryUI m_Inventory, out ItemSlot itemSlot)
        {
            itemSlot = m_Inventory.ItemSlotLib.ToList().Find(e => !e.Value.IsPlaced).Value;

            bool isFind = itemSlot != null;
            if (!isFind)
            {
                Debug.LogError("InventoryUI | 查无空缺占位");
            }

            return isFind;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="mousePosition"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public static bool TryGetItemSlot(this InventoryUI m_Inventory, Vector2 point, out ItemSlot output)
        {
            output = m_Inventory.ItemSlotLib
                .ToList()
                .Find(e => Vector3.Distance(e.Value.GetComponent<RectTransform>().localPosition, point) < e.Value.GetComponent<RectTransform>().rect.width * 0.5f)
                .Value;

            bool isFind = output != null;

            if (!isFind)
            {
                Debug.LogError($"InventoryUI | 查无槽位：{point}");
            }

            return isFind;
        }
    }
}
